package script.mvel;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.mvel2.MVEL;

public class TestMvelPerf {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		String expression = "String testStr = \"tiesheng\"; def test(){int testInt = 111;System.out.println(9999); return \"tiesheng\";} test();";

		String customerID = "123456789";
		Map<String, Object> dataModelMap = new HashMap<String, Object>();
	    Map<String, Object> tempValueMap = new HashMap<String, Object>();
	    Map<String, Object> outputValueMap = new HashMap<String, Object>();
        dataModelMap.put("AUC_COLLECT_CNT", 1.0);
        dataModelMap.put("RETURN_RATE", 26.0);
        dataModelMap.put("SELLER_NUM", 0.0);
        dataModelMap.put("SELLER_STAR_ID", 0.045);
        dataModelMap.put("SHOP_COLLECT_CNT", 1.0);  
        dataModelMap.put("CHILD_NUM", 1.0);
        dataModelMap.put("TOTAL_PAY_FEE", 1.0);
        dataModelMap.put("CAL_IPV_UV_CNT", 1.0);
        dataModelMap.put("APP_3M_CITY_CNT", 1.0);
        dataModelMap.put("LG_3M_CITY_CNT", 1.0);
        dataModelMap.put("UPLOAD_PROD_DAYS", 1.0);
        dataModelMap.put("ALL_AUC_CNT", 1.0);
        dataModelMap.put("CITY_CNT", 1.0);
        dataModelMap.put("LOGIN_IP_MAJOR_CITY_PCT", 1.0);
        dataModelMap.put("ONLINE_AUCTION_COUNT", 1.0);
        dataModelMap.put("AVG_ALIPAY_RESIDUAL_AMT", 1.0);
        dataModelMap.put("AVG_CASH_IN", 1.0);
        dataModelMap.put("AVG_CASH_OUT", 1.0);
        dataModelMap.put("CASH_IN_DAYS_CNT", 1.0);
        dataModelMap.put("CASH_OUT_AMT_FND", 1.0);
        dataModelMap.put("CASH_OUT_CNT_TRAN", 1.0);
        dataModelMap.put("CASH_OUT_DAYS_CNT", 1.0);
        dataModelMap.put("IND_1_MINOR", 1.0);
        dataModelMap.put("IND_2_SEND", 1.0);
        dataModelMap.put("IND_3_RETURN", 1.0);
        dataModelMap.put("IND_4_AMT", 1.0);
        dataModelMap.put("IND_5_CNT", 1.0);
        dataModelMap.put("TRANSFER_ACCOUNT_CNT", 1.0);
        dataModelMap.put("SUB_OTHER_ACCOUNT_CNT", 1.0);
	    //------------------------------------------------
	    dataModelMap.put("SELLER_STAR_ID", 10.0);
	    dataModelMap.put("ALL_AUC_CNT", 439.0);
	    dataModelMap.put("CASH_IN_DAYS_CNT", 165.0);
	    dataModelMap.put("CASH_OUT_CNT_TRAN", 340.0);
	    dataModelMap.put("CASH_OUT_DAYS_CNT", 183.0);
	    
		UserInfo user = new UserInfo(customerID, dataModelMap, tempValueMap, outputValueMap);
		Map<String, Object> vars = new HashMap<String, Object>();
		vars.put("user", user);
		
		expression = getExp();
		long start = System.currentTimeMillis();
		for(int i=0;i<10000;i++){
		    MVEL.eval(expression, vars);
		}
		System.out.println((System.currentTimeMillis()-start)+"ms");
	}
	
	static String getExp() throws IOException{
	    String fileContent = "";
	    FileReader fileReader = new FileReader("d:/mvel.txt");
	    BufferedReader reader = new BufferedReader(fileReader);
	    String line = "";
	    while ((line=reader.readLine())!=null) {
	        fileContent += line;
	    }
	    reader.close();
	    return fileContent;
	}

}
